package cn.glc.daily.life.web.support;

import cn.glc.daily.life.infrastructure.repository.redis.RedisRepository;
import cn.glc.daily.life.model.ro.UserTaskRo;
import cn.simon.free.tools.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 用户任务工具
 *
 * @author simon
 * @date 2021/5/31 14:19
 * @copyright:
 * @modifier
 * @motto a no talent, people who have to study hard
 */
@Slf4j
@Component
public class TaskSupport {

    public void add(UserTaskRo userTaskRo) {
        RedisRepository.redisUserTaskRepository.saveWithExpire(pushTimeValid(userTaskRo));
    }

    public void del(Long id) {
        RedisRepository.redisUserTaskRepository.delete(id);
    }

    public UserTaskRo getById(Long id) {
        return RedisRepository.redisUserTaskRepository.get(id);
    }

    public void update(UserTaskRo userTaskRo) {
        RedisRepository.redisUserTaskRepository.saveWithExpire(userTaskRo);
    }

    private UserTaskRo pushTimeValid(UserTaskRo userTaskRo) {
        if (userTaskRo.getPushTime().after(DateUtils.now())) {
            userTaskRo.setExpireTime(userTaskRo.getPushTime().getTime() - DateUtils.now().getTime());
        }
        return userTaskRo;
    }
}
